package com.example.springbootvalidated.service;
import com.example.springbootvalidated.entity.User;
import com.example.springbootvalidated.mapper.UserMapper;
import com.example.springbootvalidated.utils.ResultReturn;
import com.example.springbootvalidated.utils.ResultReturnUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
/**
 * Created by IntelliJ IDEA2022.3.2
 * @Author: Tenghw
 * @Date: 2023/05/05  15:57
 * @Description:
 */
@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    @Transactional(rollbackFor = Exception.class)
    public ResultReturn addUser(User user) {
        int insert = userMapper.insert(user);
        if(insert == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultReturnUtil.fail("添加用户出现失败");
        }
        return ResultReturnUtil.success("添加用户成功",user);
    }
}
